package fun.sboy.headacheMemo.controller;

import fun.sboy.headacheMemo.commons.lang.TokenNotFindException;
import fun.sboy.headacheMemo.util.ResultUtils;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class ExceptionControllerAdvice {

    @ExceptionHandler(TokenNotFindException.class)
    public ResultUtils tokenNotFindException(TokenNotFindException e) {
        return ResultUtils.get(401, e.getMessage(), null);
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResultUtils uniqueException(Exception e) {
        return ResultUtils.fail(e.getMessage() + " 已存在");
    }

    @ExceptionHandler(Exception.class)
    public ResultUtils exception(RuntimeException e) {
        System.out.println(e);
        return ResultUtils.fail(e.getMessage());
    }

    @ExceptionHandler(ExpiredJwtException.class)
    public ResultUtils expiredJwtException(ExpiredJwtException e) {
        return ResultUtils.get(401, "身份验证过期，token过期", null);
    }
}
